package org.one.stone.soup.grfxML.plugin.image;

import java.awt.Color;
import java.awt.Image;

import org.one.stone.soup.grfx.ImageFactory;
import org.one.stone.soup.grfxML.DataColor;
import org.one.stone.soup.grfxML.DataImage;
import org.one.stone.soup.grfxML.GeometryImage;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;

public class MakeTransparent extends SimplePlugin {

	public static final int ARG_IMAGE = 0;

	public static final int ARG_COLOR = 1;

	DataColor color = new DataColor();
	DataImage image = new DataImage();

	boolean colorDefined = false;
	Color transparentColor;
	Image imageImpl;

/**
 * SimpleImageButton constructor comment.
 */
public MakeTransparent (GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
	imageImpl = ((GeometryImage)image.getGeometry(getEngine())).getImage();
	
	if(colorDefined==true)
	{
		transparentColor = new Color(
			color.getValueX(),
			color.getValueY(),
			color.getValueZ()
		);
	}
	else
	{
		transparentColor = ImageFactory.getColorAt(imageImpl,0,0);
	}
	
	imageImpl = ImageFactory.makeColorTransparent(
						imageImpl,
						transparentColor
					);
					
	((GeometryImage)image.getGeometry(getEngine())).setImage( imageImpl );
}
/**
 * process method comment.
 */
public void process()
{
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{
	image = grfxMLCaster.cast(image,getArg(image,ARG_IMAGE,store));
	
	if(getData().size()>1)
	{
		color = grfxMLCaster.cast(color,getArg(color,ARG_COLOR,store));
		colorDefined = true;
	}
}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{
	image = grfxMLCaster.replace(image,oldObj,newObj);
	if(colorDefined==true)
	{
		color = grfxMLCaster.replace(color,oldObj,newObj);
	}
}
/**
 * stop method comment.
 */
public void stop() {}
}
